package com.yunhe.sms.controller;

import com.aliyuncs.exceptions.ClientException;
import com.google.code.kaptcha.Constants;
import com.yunhe.commons.util.NumberUtils;
import com.yunhe.commons.util.SmsUtils;
import com.yunhe.commons.util.mail.Mail;
import com.yunhe.commons.util.mail.MailUtils;
import com.yunhe.sms.entity.system.User;
import com.yunhe.sms.service.system.impl.UserServiceImpl;
import com.yunhe.webmvc.annotation.RequestBean;
import com.yunhe.webmvc.annotation.RequestMapping;
import com.yunhe.webmvc.annotation.ResponseJson;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ljh201314 on 2017/10/15.
 */
@RequestMapping("/registe")
public class RegisteController {
    private UserServiceImpl userService = new UserServiceImpl();


    @RequestMapping
    public String registe(){
        return "registe";
    }

    @RequestMapping("/emailReg")
    public String emailReg(){
        return "emailReg";
    }

    @RequestMapping("/sendMsg")
    @ResponseJson
    public Map sendMsg(HttpServletRequest req, String phone) {
        Map map = new HashMap();
        HttpSession session = req.getSession();

        if(null != phone){
            int pcode = NumberUtils.random();
            session.setAttribute("dxm",pcode);
            session.setAttribute("time",new Date());

            HashMap<String,Object> paramMap = new HashMap<>();
            paramMap.put("code",pcode);
            try {
                SmsUtils.sendSms(phone,paramMap);
                map.put("code",1);
                map.put("msg","短息验证码已发送");
            } catch (ClientException e) {
                e.printStackTrace();
                map.put("code",2);
                map.put("msg","短息验证码发送失败");
            }

        }else{
            map.put("code",0);
            map.put("msg","图片验证码输入错误或失效");
        }
        return map;
    }

    @RequestMapping("/tpmjy")
    @ResponseJson
    public String tpmjy(HttpServletRequest req, String imgYzm){
        HttpSession session = req.getSession();
        String reg_captcha = (String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
        Date reg_captcha_date = (Date)session.getAttribute(Constants.KAPTCHA_SESSION_DATE);
        long sss = new Date().getTime() - reg_captcha_date.getTime();
        String flag = "true";

        if( (sss/(1000*60)>5)|| !reg_captcha.equalsIgnoreCase(imgYzm)){
            flag = "false";
        }
        session.removeAttribute(Constants.KAPTCHA_SESSION_KEY);
        session.removeAttribute(Constants.KAPTCHA_SESSION_DATE);
        return flag;
    }

    @RequestMapping("/dxmjy")
    @ResponseJson
    public String dxjy(HttpServletRequest req, String msgYzm){

        HttpSession session = req.getSession();
        /*Date date = (Date)session.getAttribute("time");
        if(null != date){
            long ss = (new Date().getTime() - date.getTime())/(1000*60);
            if(ss>=5){
                session.removeAttribute("dxm");
            }
        }*/
        Object dxYzm1 = session.getAttribute("dxm");
        String J=String.valueOf(dxYzm1);

        String flag = "true";
        if(!msgYzm.equals(J)){
            flag = "false";
        }
       return flag;
    }


    @RequestMapping("/sendEmail")
    @ResponseJson
    public Map sendEmail(HttpServletRequest req, @RequestBean User user){

        Map map = new HashMap();

        HttpSession session = req.getSession();
        session.setAttribute("emails",user.getMail());

        String url =   "http://172.16.8.12/user/save?user="+user;

        String contents = "请点击以下链接设置激活账号：<br><a href=\""+url+"\">点击此链接登录账号</a>";

        Mail mail = new Mail("2603459843@qq.com", user.getMail(), "账号激活", contents);
        try {
            MailUtils.mail(mail);
            map.put("msg","邮件发送成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

}
